package com.hnhs.enums;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Optional;

public enum ProductStatus {
    ONLINE(1, "上线"),
    OFFLINE(5, "下线"),
    DISABLE(10, "禁售");

    @Getter
    @Setter
    private Integer code;
    @Getter
    @Setter
    private String msg;

    ProductStatus(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ProductStatus findByCode(Integer code) {
        if(code == null){
            return  null;
        }
        //你传过来的枚举 我给你导出来
        //Optional 空处理函数
        Optional<ProductStatus> first = Arrays.stream(ProductStatus.values()).filter(item -> item.getCode().equals(code)).findFirst();
        //如果first 有值就返回
        if (first.isPresent()) {
            return first.get();
        }
        //如果没有值 就返回null 给定的默认值
        return first.orElse(null);
    }

}
